Skip to content

Conversation

@Timeless0911
Copy link
Contributor

@Timeless0911 Timeless0911 commented Dec 12, 2025

Highlights 💡

New JavaScript API

This release introduces a new, comprehensive JavaScript API for Rslib.

Caution

This is a Breaking Change. The previously exposed informal API has been removed. You can switch to the new JavaScript API and refer to the docs below to re-integrate.

See the API docs for details:

Usage

  1. Install Rslib: install the @rslib/core package:
npm add @rslib/core -D
  1. Create an Rslib instance: call the createRslib method to create an Rslib instance:
import { createRslib } from '@rslib/core';

const rslib = await createRslib();
  1. Call Rslib instance methods: use the rslib.build method which will build production outputs:
await rslib.build();

Summary

  • Provide a comprehensive JavaScript API

    Type:

     type RslibInstance = {
         build(options?: BuildOptions): Promise<BuildResult>;
         inspectConfig(options?: InspectConfigOptions): Promise<InspectConfigResult>;
         startMFDevServer(options?: StartMFDevServerOptions): Promise<StartServerResult>;
         getRslibConfig(): Readonly<RslibConfig>;
         onAfterCreateRsbuild(callback: OnAfterCreateRsbuildFn): void;
     };
     
     function createRslib(options?: CreateRslibOptions): Promise<RslibInstance>;

    Example:

     const rslib = await createRslib({
       config: {
         // Rslib configuration
       },
     });
    
     await rslib.build();
  • Refactor public API

    Core methods:

    • runCLI
    • createRslib
    • defineConfig
    • loadConfig
    • loadEnv
    • mergeRslibConfig
  • Verbose Rslib/Rsbuild/Rspack configurations when running with DEBUG=rslib

  • Add --no-env cli options to control disable loading of .env files after refactor

Related Links

close: #1040
close: #967

Checklist

  • PR Description
  • Tests updated (or not required).
  • JS Doc
  • Documentation updated (or not required).
  • Breaking changes and release notes

Copilot AI review requested due to automatic review settings December 12, 2025 08:56
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the CLI initialization and error handling to improve user experience. The main changes include renaming the public API from runCli to runCLI (following proper casing convention), consolidating CLI setup logic, and implementing smarter output formatting for different package managers. The refactor also adds a process title for easier identification and simplifies build error logging.

Key changes:

  • Renamed public API runCli to runCLI and consolidated CLI initialization logic into a new /cli/index.ts file
  • Removed the prepareCli function and integrated its functionality into the main CLI entry point
  • Enhanced package manager detection to handle output formatting for npx, Bun, and Node.js --run command
  • Introduced RSPACK_BUILD_ERROR constant to conditionally suppress redundant error messages
  • Renamed internal function initConfig to init for better clarity

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
packages/core/src/index.ts Updated public API exports: renamed runCli to runCLI and removed prepareCli export
packages/core/src/cli/prepare.ts Deleted file - functionality moved to /cli/index.ts
packages/core/src/cli/init.ts Renamed function from initConfig to init
packages/core/src/cli/index.ts New file consolidating CLI initialization with improved package manager detection and greeting logic
packages/core/src/cli/commands.ts Added RSPACK_BUILD_ERROR constant and error filtering logic; renamed import from initConfig to init; updated import path
packages/core/bin/rslib.js Simplified entry point to call runCLI directly without try-catch wrapper

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Timeless0911 Timeless0911 marked this pull request as draft December 12, 2025 09:20
@Timeless0911 Timeless0911 changed the title refactor!: enhance CLI behaviour and logs feat!: add JavaScript API and enhance CLI behaviour Dec 15, 2025
@netlify
Copy link

netlify bot commented Dec 17, 2025

Deploy Preview for rslib ready!

Name Link
🔨 Latest commit 08eb493
🔍 Latest deploy log https://app.netlify.com/projects/rslib/deploys/694cf66ba54b250008f29a03
😎 Deploy Preview https://deploy-preview-1394--rslib.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@Timeless0911 Timeless0911 force-pushed the js-api branch 7 times, most recently from 8320ba4 to e2d7a37 Compare December 24, 2025 08:53
@Timeless0911 Timeless0911 marked this pull request as ready for review December 24, 2025 08:53
@Timeless0911 Timeless0911 force-pushed the js-api branch 3 times, most recently from 59330fc to f61d16c Compare December 24, 2025 10:06
await rslib.build();
```

> 关于 Rslib 实例方法的更多介绍,请阅读 [Rslib instance](/api/javascript-api/instance) 章节。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not a quote, consider using ::: syntax


const mergedConfig = mergeRslibConfig(config1, config2);

console.log(mergedConfig);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about console log the merged config here


```ts
// the same as `import { rspack } from '@rspack/core'`
import { rspack } from '@rslib/core';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use two slash to annotate the type here?

const { rslibConfig, rsbuildConfig, bundlerConfigs } =
await rslib.inspectConfig();

console.log(rslibConfig, rsbuildConfig, bundlerConfigs);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meaning less to console if we don't show what it will be like.

在非 watch 模式下,你也应该调用 `close()` 方法来结束构建,这会触发 Rsbuild 的 [onCloseBuild](https://rsbuild.rs/zh/plugins/dev/hooks#onclosebuild) 钩子,执行清理操作。

```ts
const buildResult = await rslib.build();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about renaming to rslibInstance, calling close method on a result seems weird.

await buildResult.close();
```

在非 watch 模式下,你也应该调用 `close()` 方法来结束构建,这会触发 Rsbuild 的 [onCloseBuild](https://rsbuild.rs/zh/plugins/dev/hooks#onclosebuild) 钩子,执行清理操作。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so onCloseBuild will be called N + 1 times, if i manually invoked it N times?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is align with rsbuild.build, the result is the return value of rsbuild.build, see https://rsbuild.rs/api/javascript-api/instance#close-build

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it's fine. just curious.

Rslib 实例的类型,对应 [createRslib](/api/javascript-api/core#createrslib) 方法的返回值。

```ts
import type { RslibInstance } from '@rslib/core';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two slash?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: expose stats result for build command by js api [Feature]: stabilize public JS API

4 participants